<?php

namespace IDock\Insurance\JDAL\Request;

use Exception;
use IDock\Insurance\JDAL\Bean\BasePremiumInquiry;
use IDock\Insurance\JDAL\JDALEnums;
use IDock\Insurance\JDAL\Response\VehicleInsureQueryResponse;
use IDock\Insurance\JDAL\Response\VehicleQuoteMainResponse;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\Premium\PremiumChargingPileVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;

class VehicleQuoteMainRequest extends AllJDALRequest
{
    public $route = 'quote/create';

    public $requestCode = 'quoteCreateRequest';

    protected $response = VehicleQuoteMainResponse::class;

    protected $urlTip = '报价接口';

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    /*** @var BasePremiumInquiry $basePremiumInquiry */
    public $basePremiumInquiry;

    /**
     * @var VehicleInsureQueryResponse  $queryResponse
     */
    public $queryResponse;

    /**
     * @var VehicleInsureQueryResponse $recommendResponse
     */
    public $recommendResponse;

    private $generateData = [];

    /**
     * @return array
     * @throws Exception
     */
    public function generate(): array
    {
        $this->partnerRef = $this->basePremiumInquiry->partnerRef;

        if ($this->generateData) {
            return $this->generateData;
        }

        $data = [
            'basePart' => $this->basePart(),
            'vehicle' => $this->vehicle(),
            'owner' => $this->basePremiumInquiry->owner,
            'policyParties' => $this->basePremiumInquiry->policyParties,
            'coverageItemList' => $this->basePremiumInquiry->coverageItemList,
            'specialPromiseInfoList' => [],
            'channel' => $this->channel(),
        ];

        //充电桩
        if ($this->visitor->getChargingPileVisitorList()) {
            $data['chargingPostDataList'] = $this->chargingPostDataList();
        }

        //车船税
        if ($this->visitor->getCarTaxVisitor()->taxTypeCode != 'T01') {
            $data['vehicleTaxation'] = $this->basePremiumInquiry->vehicleTaxation;
        }

        //驾乘险
        if ($this->visitor->getNonCarInquiryVisitor()->getNonCarList()) {
            $data['drivingAccidentInfo'] = $this->drivingAccidentInfo();
        }

        $data['quoteOperateType'] = 'CONFIRM';

        $this->generateData = $data;

        return $data;
    }

    /**
     * 基本信息
     * @return array
     */
    private function basePart(): array
    {
        $basePart = $this->basePremiumInquiry->basePart;

        $basePart['commEffectFlag'] = 'N';  //商业险即时起保标志
        $basePart['ctplEffectFlag'] = 'N';  //交强险即时起保标志
        $basePart['uwDiscountValue'] = '0';  //自主定价系数
        //$basePart['solutionCode'] = ''; //方案编码

        if ($this->visitor->getBiStartData()) {
            if (date('H', strtotime($this->visitor->getBiStartData())) != '00') {
                $basePart['commEffectFlag'] = 'Y';
            }
        }

        if ($this->visitor->getCiStartData()) {
            if (date('H', strtotime($this->visitor->getCiStartData())) != '00') {
                $basePart['ctplEffectFlag'] = 'Y';
            }
        }

        $basePart['applicationNoType'] = '1';

        //出单员code
        $basePart['staffCode'] = $this->visitor->getExtInfoVisitor()->argc['staffCode'] ?? '';

        if ($this->visitor->getExtInfoVisitor()->selfPricingFactor) {
            $basePart['uwDiscountValue'] = $this->visitor->getExtInfoVisitor()->selfPricingFactor;
        }

        return $basePart;
    }

    /**
     * 车辆信息
     * @return array
     */
    private function vehicle(): array
    {
        $vehicle = $this->basePremiumInquiry->vehicle;

        $vehicle['commVehSubtype'] = $this->basePremiumInquiry->commVehSubtype;

        $modelCode = $this->recommendResponse->modelCode ?: $this->queryResponse->modelCode;

        if($modelCode) {
            $vehicle['modelCode'] = $modelCode;
        }

        $vehicleStyleUniqueId = $this->recommendResponse->vehicleStyleUniqueId ?: $this->queryResponse->vehicleStyleUniqueId;

        if ($vehicleStyleUniqueId) {
            $vehicle['vehicleStyleUniqueId'] = $vehicleStyleUniqueId;
        }

        return $vehicle;
    }

    /**
     * 渠道信息
     * @return array
     */
    private function channel(): array
    {

        return [
            'businessType' => $this->visitor->getExtInfoVisitor()->argc['businessType'] ?? '',
            'channelCode' => $this->visitor->getExtInfoVisitor()->argc['channelCode'] ?? '',
            'channelName' => $this->visitor->getExtInfoVisitor()->argc['channelName'] ?? '',
            'salesIdNo' => $this->visitor->getExtInfoVisitor()->argc['salesIdNo'] ?? '',
            'salesLicenseNo' => $this->visitor->getExtInfoVisitor()->argc['salesLicenseNo'] ?? '',
            'salesName' => $this->visitor->getExtInfoVisitor()->argc['salesName'] ?? '',
            'salesTelNo' => $this->visitor->getExtInfoVisitor()->argc['salesTelNo'] ?? '',
        ];
    }

    /**
     * 充电桩信息
     * @return array
     */
    private function chargingPostDataList(): array
    {
        $list = [];

        /*** @var $value PremiumChargingPileVisitor */
        foreach ($this->visitor->getChargingPileVisitorList() as $value) {
            $item = [
                'chargingPostType' => $value->chargingPileType,
                'chargingPostCode' => $value->chargingPileCode,
                'chargingPostAddress' => $value->chargingPileAddress,
                'chargingPostAddressType' => JDALEnums::$chargingPileAddressType[$value->chargingPileAddressType],
                'chargingPostKind' => JDALEnums::$chargingPileKind[$value->chargingPileKind],
                'chargingPostYearLimit' => JDALEnums::$chargingPileYearLimit[$value->chargingPileYearLimit],
                'lossSumLimit' => $value->lossAmount,
                'liabilitySumLimit' => $value->liabilityAmount,
            ];

            $list[] = $item;
        }

        return $list;
    }

    /**
     * 非车
     * @return array
     */
    private function drivingAccidentInfo(): array
    {
        /*** @var $nonCarProductVisitor NonCarProductVisitor */
        $nonCarProductVisitor = $this->visitor->getNonCarInquiryVisitor()->getNonCarList()[0];

        $beginDate = $this->visitor->biStartData ?: $this->visitor->ciStartData;

        if (date('H', strtotime($beginDate)) != '00') {
            $beginDate = date('Y-m-d 00:00:00', strtotime(' +1day', strtotime($beginDate)));
        }

        $endDate = date('Y-m-d H:i:s',strtotime('+1 year',strtotime($beginDate)));

        $data = [
            'effectiveDate' => $beginDate,
            'expireDate' => $endDate,
            'planCode' => $nonCarProductVisitor->planCode,
            'productLine' => $nonCarProductVisitor->productCode,
        ];

        return $data;
    }
}
